package com.example.cet4try.MyFragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.activity.ActivityWordList;
import com.example.cet4try.diyView.CirclePercentView;
import com.example.cet4try.adapter.PageListAdapter;

import java.util.Calendar;

public class MyFragment2 extends Fragment {

    private String username;
    private CirclePercentView progressView;
    private TextView tv_percentage,db_percentage;
    private TextView riqi,zhengquelv,lianxicishu;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.page2,container,false);

        SharedPreferences preferences = getActivity().getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");
        progressView = v.findViewById(R.id.progressView);
        tv_percentage = v.findViewById(R.id.percentage);
        db_percentage = v.findViewById(R.id.db_percentage);
        riqi = v.findViewById(R.id.riqi);
        zhengquelv = v.findViewById(R.id.zhengquelv);
        lianxicishu = v.findViewById(R.id.lianxicishu);

        initPercentage();

        if (username.equals("yk")){
            v.findViewById(R.id.lx_data).setVisibility(View.GONE);
        }

        ListView p2list = v.findViewById(R.id.p2list);
        PageListAdapter p2ListAdapter = new PageListAdapter(new String[]{"已学习","未学习","已学会"},getActivity());
        p2list.setAdapter(p2ListAdapter);
        p2list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ActivityWordList.class);
                switch (position){
                    case 0:
                        intent.putExtra("type","learned");
                        startActivity(intent);
                        break;
                        case 1:
                        intent.putExtra("type","remain");
                        startActivity(intent);
                        break;
                    case 2:
                        intent.putExtra("type","already");
                        startActivity(intent);
                        break;
                    default:break;
                }
            }
        });

        return v;
    }

    private void initPercentage(){
        int count_learned = Tools.CountUserData(username,"learned");
        String dbname = "四级单词:        "+count_learned+"/";
        float db_count = 4493f;
        float percentage = ((int)((count_learned/db_count)*10000))/100f;
        progressView.setPercentage(percentage);
        tv_percentage.setText(percentage+"%");
        db_percentage.setText(dbname+(int)db_count);
        Calendar calendar = Calendar.getInstance();
        riqi.setText(calendar.get(Calendar.YEAR)+"."+(calendar.get(Calendar.MONTH)+1)+"."+calendar.get(Calendar.DAY_OF_MONTH));
//        int count_wrong = Tools.CountUserData(username,"wrong");
        SharedPreferences u_preferences = getActivity().getSharedPreferences(username+"_Shared_Data", 0);
        int times = u_preferences.getInt("numoftimes",0);
        int rights = u_preferences.getInt("right_times",0);
        lianxicishu.setText(String.valueOf(times));
        if (times == 0)
            times = 1;
        if (rights == 0)
            rights = 1;
        percentage = ((int)((rights/(times*1f))*10000))/100f;
        zhengquelv.setText(percentage+"%");

    }

//Toast.makeText(getContext(),"刷新数据",Toast.LENGTH_SHORT).show();
        @Override
        public void onResume() {
//            Log.e("MyFragment3", "刷新" );
            initPercentage();
            super.onResume();
        }




}
